<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="服务员id" prop="waiterId">
      <el-input v-model="dataForm.waiterId" placeholder="服务员id"></el-input>
    </el-form-item>
    <el-form-item label="房价id" prop="floorRoomId">
      <el-input v-model="dataForm.floorRoomId" placeholder="房价id"></el-input>
    </el-form-item>
    <el-form-item label="创建人id" prop="creatUserId">
      <el-input v-model="dataForm.creatUserId" placeholder="创建人id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="ceeatTime">
      <el-input v-model="dataForm.ceeatTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remack">
      <el-input v-model="dataForm.remack" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="商家id" prop="sellerId">
      <el-input v-model="dataForm.sellerId" placeholder="商家id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          waiterId: '',
          floorRoomId: '',
          creatUserId: '',
          ceeatTime: '',
          remack: '',
          sellerId: ''
        },
        dataRule: {
          waiterId: [
            { required: true, message: '服务员id不能为空', trigger: 'blur' }
          ],
          floorRoomId: [
            { required: true, message: '房价id不能为空', trigger: 'blur' }
          ],
          creatUserId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          ceeatTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          remack: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '商家id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/twaiterrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.waiterId = data.tWaiterRecord.waiterId
                this.dataForm.floorRoomId = data.tWaiterRecord.floorRoomId
                this.dataForm.creatUserId = data.tWaiterRecord.creatUserId
                this.dataForm.ceeatTime = data.tWaiterRecord.ceeatTime
                this.dataForm.remack = data.tWaiterRecord.remack
                this.dataForm.sellerId = data.tWaiterRecord.sellerId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/twaiterrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'waiterId': this.dataForm.waiterId,
                'floorRoomId': this.dataForm.floorRoomId,
                'creatUserId': this.dataForm.creatUserId,
                'ceeatTime': this.dataForm.ceeatTime,
                'remack': this.dataForm.remack,
                'sellerId': this.dataForm.sellerId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
